package com.jikangjun.disk.role.service.impl;

import com.jikangjun.disk.menu.exception.MenuOccupyException;
import com.jikangjun.disk.mybatis.entity.PtRoleEntity;
import com.jikangjun.disk.mybatis.flexigrid.PagingEntity;
import com.jikangjun.disk.mybatis.flexigrid.TblResponseEntity;
import com.jikangjun.disk.mybatis.ztree.ZtreeEntity;
import com.jikangjun.disk.role.entity.SearchRoleEntity;
import com.jikangjun.disk.role.repository.RoleDao;
import com.jikangjun.disk.role.service.RoleService;
import com.jikangjun.disk.user.exeption.UserOccupyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;

    @Override
    public TblResponseEntity<PtRoleEntity> queryRoleInfoListByPage(SearchRoleEntity entity, PagingEntity paging) {
        TblResponseEntity<PtRoleEntity> tblResponseEntity = new TblResponseEntity<>();
        Integer total = roleDao.queryRoleListTotal(entity);
        tblResponseEntity.setTotal(total);

        paging.deal(PtRoleEntity.class,total);
        tblResponseEntity.setPage(paging.getPage());
        List<PtRoleEntity> list = roleDao.queryRoleInfoListByPage(entity,paging);
        tblResponseEntity.setRawRecords(list);

        tblResponseEntity.deal();

        return tblResponseEntity;

    }
    @Override
    public PtRoleEntity queryRoleInfoById(String roleUuid) {
        return roleDao.queryRoleInfoById(roleUuid);
    }

    @Override
    public void modifyRoleInfoById(PtRoleEntity entity) {
        roleDao.modifyRoleInfoById(entity);
    }

    @Override
    public void addRoleInfoById(PtRoleEntity entity) {
        roleDao.addRoleInfoById(entity);
    }

    @Override
    public void deleteRoleByIds(String ids) throws UserOccupyException, MenuOccupyException {
        String roleArray[] = ids.split(",");
        String roleName = null;
        StringBuffer userSb = new StringBuffer();
        StringBuffer menuSb = new StringBuffer();
        for(String roleUuid:roleArray){
            roleName = roleDao.queryUserListByRole(roleUuid);
            if(roleName != null){
                userSb.append(roleName);
                userSb.append(",");
            }

            roleName = roleDao.queryMenuListByRole(roleUuid);
            if(roleName != null){
                menuSb.append(roleName);
                menuSb.append(",");
            }
        }

        String userStr= userSb.toString();
        if(!"".equals(userStr)){
            userStr = userStr.substring(0,userStr.length() - 1);
            throw  new UserOccupyException(userStr);
        }

        String menuStr= menuSb.toString();
        if(!"".equals(menuStr)){
            menuStr = menuStr.substring(0,menuStr.length() - 1);
            throw  new MenuOccupyException(menuStr);
        }

        roleDao.deleteRoleByIds(roleArray);
    }

    @Override
    public List<ZtreeEntity> queryMenuTreeListByRole(String roleUuid) {
        List<ZtreeEntity> list = roleDao.queryMenuTreeList();
        List<String> menuList = roleDao.queryMenuByRole(roleUuid);
        for(ZtreeEntity ztreeItem:list){
            for(String menuId:menuList){
                if(menuId.equals(ztreeItem.getId())){
                    ztreeItem.setChecked(true);
                }
            }
        }
        return list;
    }

    @Override
    public void saveRoleRefMenu(String roleUuidArray, String menuIdArray) {
        String[] roleUuidList = roleUuidArray.split(",");
        String[] menuIdList = menuIdArray.split(",");
        for (String roleUuid:roleUuidList){
            roleDao.deleteMenuByRole(roleUuid);
            for (String menuId:menuIdList){
                roleDao.insertMenuRefRole(roleUuid,menuId);
            }
        }
    }
}
